package io.github.wanggit.antrpc.client.config.workers;

import io.github.wanggit.antrpc.IAntrpcContext;
import io.github.wanggit.antrpc.commons.config.ConfigChangeTypeEnum;
import io.github.wanggit.antrpc.commons.config.TelnetConfig;
import io.vavr.collection.List;

public class TelnetConfigChangeWorker extends AbstractChangeWorker {

    public TelnetConfigChangeWorker(IAntrpcContext antrpcContext, List<String> prefixs) {
        super(antrpcContext, prefixs);
    }

    @Override
    protected synchronized void internalFired(
            ConfigChangeTypeEnum changeType, String key, String oldValue, String newValue) {
        TelnetConfig telnetConfig = getAntrpcContext().getConfiguration().getTelnetConfig();
        setFieldValue(telnetConfig, key, newValue);
        if (null != telnetConfig.getEnable() && telnetConfig.getEnable()) {
            getAntrpcContext()
                    .restartTelnetServerIfRunning(
                            getAntrpcContext().getConfiguration(),
                            getAntrpcContext().getSpringApplicationContext());
        } else {
            getAntrpcContext()
                    .stopTelnetServerIfRunning(
                            getAntrpcContext().getConfiguration(),
                            getAntrpcContext().getSpringApplicationContext());
        }
    }
}
